<?php
namespace app\house\controller;
use app\AdminRest;
use app\ApiRest;
use app\house\model\AdminHoseStaff;
use app\house\model\LongPage;
use app\house\model\LongPageRecord;
use think\App;
use think\facade\Db;
use think\Request;
use app\house\model\IndexHouse as house_model;
use app\house\model\AdminBanner;
use app\house\model\AdminCate;
use app\house\model\AdminArea;
use app\house\model\AdminPrice;
use app\house\model\IndexAppointment;
use app\house\model\IndexCollect;
use app\house\model\AdminApartment;


class IndexHouse extends ApiRest{
    protected $model;
    protected $banner_model;
    protected $cate_model;
    protected $app_model;
    protected $collect_model;
    protected $area_model;
    protected $price_model;
    protected $partment_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new house_model();
        $this->banner_model = new AdminBanner();
        $this->cate_model   = new AdminCate();
        $this->app_model    = new IndexAppointment();
        $this->collect_model= new IndexCollect();
        $this->area_model   = new AdminArea();
        $this->price_model  = new AdminPrice();
        $this->partment_model = new AdminApartment();
    }
    /**
     * 房源列表
     */
    public function houseList(){
        $input = $this->_input;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        if(!empty($input['name'])){
            $dis[] = ['title','like',"%".$input['name']."%"];
        }
        if(!empty($input['cate'])){
            $dis[] = ['category','=',$input['cate']];
        }
        if(!empty($input['price'])){
            $ids   = $this->houseId($input['price']);
            $dis[] = ['id','in',$ids];
        }
        if(!empty($input['area'])){
            $area = $this->area_model->getLevelId([$input['area']]);
            $dis[] = ['area_id','in',$area];
        }
        if(!empty($input['huxing'])){
            $input['huxing'] = array_column($input['huxing'],'id');
            $dis[] = ['huxing_id','in',$input['huxing']];
        }
        $data = $this->model->houseList($dis);
        return $this->success($data);
    }
    /**
     * @throws \think\exception\DbException
     * 员工端房源列表
     */
    public function staffHouseList(){
        $input = $this->_input;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        if(!empty($input['cate'])){
            $dis[] = ['category','=',$input['cate']];
        }
        $data['cate']  = $this->cate_model->cateSelect(['uniacid'=>$this->_uniacid]);
        $data['house'] = $this->model->houseList($dis);
        if(!empty($data['house']['data'])){
            foreach ($data['house']['data'] as &$v){
                $v['app_count'] = $this->app_model->appCount(['uniacid'=>$this->_uniacid,'house_id'=>$v['id'],'to_uid'=>$this->getUserId(),'status'=>1]);
            }
        }
        return $this->success($data);
    }
    /**
     *
     * 员工端房源详情
     */
    public function staffHouseInfo(){
        $input         = $this->_input;
        $dis['id']     = $input['id'];
        $dis['status'] = 1;
        $data['info']  = $this->model->houseInfo($dis);
        if(empty($data['info'])){
            return $this->error('房源已被删除');
        }
        $data['app'] = $this->app_model->appList(['a.uniacid'=>$this->_uniacid,'a.status'=>1,'a.to_uid'=>$this->getUserId(),'a.house_id'=>$input['id']]);
        return $this->success($data);
    }

    /**
     *
     * 房源首页
     */
    public function houseIndex(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        $data['house']  = $this->model->houseList($dis);
        $data['banner'] = $this->banner_model->bannerSelect($dis);
        $data['cate']   = $this->cate_model->cateSelect($dis);
        return $this->success($data);
    }
    /**
     * @param $data
     * @return array
     * 获取houseid
     */
    public function houseId($data){
        $arr = array();
        foreach ($data as $v){
            $min = $v['min'];
            $max = $v['max'];
            $dis = array();
            $dis[] = ['uniacid','=',$this->_uniacid];
            $dis[] = ['status','=',1];
            $dis[] = ['price','between',"$min,$max"];
            $ids   = $this->model->houseId($dis);
            $arr   = array_merge($arr,$ids);
        }
        return !empty($arr)?$arr:['not id'];
    }
    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查看房源详情
     */
    public function houseInfo(){
        $input         = $this->_input;
        $dis['id']     = $input['id'];
        $dis['status'] = 1;
        $staff_id      = !empty($input['staff_id'])?$input['staff_id']:'';
        $data['info']  = $this->model->houseInfo($dis,$staff_id);
        if(empty($data['info'])){
            return $this->error('房源已被删除');
        }
        $collect = $this->collect_model->collectInfo(['house_id'=>$input['id'],'uid'=>$this->getUserId()]);
        $data['info']['is_collect'] = !empty($collect)?1:0;
        $di[] = ['uniacid','=',$this->_uniacid];
        $di[] = ['id','<>',$input['id']];
        $di[] = ['status','=',1];
        $data['list'] = $this->model->houseList($di);
        $data['info']['staff']=Db::name('longbing_card_user_info')
                        ->alias('a')
                        ->leftJoin('longbing_card_job b','b.id = a.job_id')
                        ->where(['a.fans_id'=>$input['staff_id']])
                        ->field('a.avatar,a.phone,a.name,a.id,b.name as job_name')
                        ->find();
        $data['info']['staff']    = transImages($data['info']['staff'],['avatar']);
       // $data['info']['house_qr'] = $this->houseQr($input['id'],$input['staff_id']);


        $data['covers'] = [
          'width'    => 28,
          'height'   => 28,
          'latitude' => $data['info']['latitude'],
          'longitude'=> $data['info']['longitude'],
          'iconPath' =>'https://lbqnyv2.migugu.com/images/12/2018/11/A33zQycihMM33y337LH23myTqTl3tl.png',
          'callout'  => [
              'fontSize'    => 14,
              'bgColor'     => '#ffffff',
              'padding'     => 4,
              'display'     => 'ALWAYS',
              'extAlign'    => 'center',
              'borderRadius'=> 2,
              'content'     => $data['info']['address']
          ],
        ];

        //浏览列表
        if($data['info']['house_type']==2){

            $dis = [
                //uniacid
                'a.uniacid' => $this->_uniacid,
                //员工
                'a.to_uid'  => $staff_id,
                //房源id
                'a.target'  => $input['id']
            ];
            //查询
            $data['pv_info'] = $this->model->pvList($dis);
        }


        return $this->success($data);
    }


    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取房源筛选配置
     */
    public function houseSetting(){
        $data['area']  = $this->area_model->areaList(['uniacid'=>$this->_uniacid]);
        $data['price'] = $this->price_model->priceSelect(['uniacid'=>$this->_uniacid]);
        $data['huxing']= $this->partment_model->apartmentSelect(['uniacid'=>$this->_uniacid]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 15:55
     * @return void
     * descption:获取房源二维码
     */
    public function houseQr($id,$stff_id){
        $house_qr    = getCache($this->getUserId().'-'.$stff_id.'-'.$id.'-'.'house_qr');
        $input['id']       = $id;
        $input['staff_id'] = $stff_id;
        $input['pid']      = $this->getUserId();
        $input['type']     = 11;
        if(empty($house_qr)){
            $data     = longbingCreateWxCode($this->_uniacid,$input,'pages/house/detail');
            $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);
            $house_qr = $data['qr_path'];
            setCache($this->getUserId().'-'.$stff_id.'-'.$id.'-'.'house_qr',$house_qr);
        }
        return $house_qr;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 16:10
     * @功能说明:房源员工推荐列表
     */
    public function houseStaffList(){

        $input         = $this->_input;

        $house_staff = new AdminHoseStaff();

        $data = $house_staff->getStaffList(['a.house_id'=>$input['id']]);

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-02-19 14:27
     * @功能说明:房源设置
     */
    public function houseSettingConfig(){


        $data  = lbPulgSettingInfo($this->_uniacid,'card',['house_style']);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:19
     * @功能说明:长页面列表
     */
    public function longPageList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $long_model = new LongPage();

        $data = $long_model->dataList($dis,10);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:23
     * @功能说明:长页面详情
     */
    public function longPageInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $long_model = new LongPage();

        $res = $long_model->dataInfo($dis);

        $long_record_model = new LongPageRecord();

        $dis = [

            'house_id' => $input['id'],

            'user_id'  => $this->getUserId()
        ];

        $find = $long_record_model->dataInfo($dis);

        $res['is_sign'] = !empty($find)?1:0;

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-13 14:35
     * @功能说明:
     */
    public function longPageRecordApp(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $long_model = new LongPageRecord();

        $res = $long_model->dataAdd($input);

        return $this->success($res);

    }





}
